/******************************************************************************/
package jamie.org.sil.www.usxConverter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;

import jamie.org.sil.www.usxUtils.*;


/******************************************************************************/
/**
 * A base class from which converters-to-xxx-from-usx and
 * converters-to-usx-from-xxx inherit.
 * <p>
 * 
 * It is convenient to present a uniform interface to the main program, and
 * this is the purpose of the present class.
 * <p>
 * 
 * The class carries out a certain amount of initialisation, and also
 * provides common processing for relatively complex undertakings, such as
 * running external commands.
 * <p>
 * 
 * The major part of the processing, however, is naturally concerned with
 * the actual conversion process, and is carried out by a method called
 * "process" which the individual converters need to implement.
 * <p>
 * 
 * You may rely upon this code or the code above it having done all the
 * "standard" work of configuration, etc.
 * <p>
 * 
 * As regards error handling, classes which extend the present one will
 * almost certainly need to handle the standard "abandon file" processing
 * discussed in {@link IncidentHandler}, but can probably ignore the
 * "abandon run" processing, on the basis that handling for this is
 * already in place higher up in the code.
 * 
 * @author ARA "Jamie" Jamieson
**/

public abstract class UsxConverter
{
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                                 Public                                 **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /**
     Carries out initialisation.
  */

  public void initialise () throws Exception
  {
  }


  /****************************************************************************/
  /**
     Runs over all input files doing processing.

     @throws Exception
   */

  abstract public void process () throws Exception;


  /****************************************************************************/
  /**
     Carries out termination processing.
  */

  public void terminate () throws Exception
  {
  }




  
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                               Protected                                **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /* Runs any external commands. */

  protected final void runExternalCommands () throws IOException, java.lang.InterruptedException
  {
    ArrayList<String> commands = Config.instance().getPostProcessorCommand();
    if (commands.size() > 0) LogManager.instance().information("Internal processing complete.  Running external commands.");
    for (int i = 0; i < commands.size(); ++i)
    {
      String cmd = commands.get(i).trim();
      if (0 != cmd.length())
      {
        Runtime run = Runtime.getRuntime();
        LogManager.instance().information("Running: '" + cmd + "'");
        Process pr = run.exec(cmd);
	//  pr.waitFor();
        BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = "";
        while (null != (line=buf.readLine())) System.out.println(line);
      }
    }
  }
}
